/**
 * 	 DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *   @author Colin Redmond
 *   Copyright (C) 2011 Colin Redmond
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */

package com.cred.industries.platform.business.facade {
	
	import com.cred.industries.platform.business.TaskEvent;
	import com.cred.industries.platform.business.object.CustomerBO;
	import com.cred.industries.platform.dao.SessionDAO;
	import com.cred.industries.platform.dao.event.SessionDAOEvent;
	import com.cred.industries.platform.model.LocalUserData;
	
	import flash.events.EventDispatcher;
	
	[Event(name="TASK_COMPLETE", type="com.cred.industries.business.TaskEvent")]
	[Event(name="TASK_FAILED", type="com.cred.industries.business.TaskEvent")]

	/**
	 * @author Colin Redmond
	 * Copyright 2011
	 **/
	public final class SessionFacade extends EventDispatcher {
		
		private var mSessionDAO:SessionDAO = new SessionDAO();
		public function SessionFacade() { }
		
		public function login(customer:CustomerBO):void {
			
			LocalUserData.get().localUser = customer
			
			if(customer == null) {
				
				trace("SessionFacade.login localUser not set");
				return;
			}
			
			mSessionDAO.addEventListener(SessionDAOEvent.SESSION_CREATED, sessionCreatedHandler);
			mSessionDAO.addEventListener(SessionDAOEvent.SESSION_FAILED, sessionFailedHandler);
			
			mSessionDAO.authenticate(customer);
		}
		
		public function logout():void {
			
			var sessionKey:String = LocalUserData.get().sessionKey;
			
			if(sessionKey == "") {
				
				trace("SessionFacade.login localUser not set");
				return;
			}
			
			mSessionDAO.addEventListener(SessionDAOEvent.SESSION_LOGGED_OUT, sessionLogoutHandler);
			mSessionDAO.addEventListener(SessionDAOEvent.SESSION_FAILED, sessionFailedHandler);
			
			mSessionDAO.logOut(sessionKey);
		}
		
		private function sessionCreatedHandler(event:SessionDAOEvent):void {
			
			var sessionId:String = event.sessionData.@sessionId;
			var custId:int = event.sessionData.customerId;
			LocalUserData.get().sessionKey = sessionId;
			LocalUserData.get().localUser.customerId = custId;
			dispatchEvent(new TaskEvent(TaskEvent.TASK_COMPLETE));
		}
		
		private function sessionFailedHandler(event:SessionDAOEvent):void {
			
			LocalUserData.get().sessionKey = "";
			dispatchEvent(new TaskEvent(TaskEvent.TASK_FAILED, event.message));
		}
		
		private function sessionLogoutHandler(event:SessionDAOEvent):void {
			
			LocalUserData.get().sessionKey = "";
			dispatchEvent(new TaskEvent(TaskEvent.TASK_COMPLETE));
		}
	}
}